package com.ycg.biquge.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ycg.biquge.common.Result;
import com.ycg.biquge.pojo.Novel;
import com.ycg.biquge.pojo.vo.Recommend;
import com.ycg.biquge.pojo.vo.SearchInfo;
import com.ycg.biquge.service.NovelService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author YCG
 * @date 2024-05-11 19:35
 * @description 搜索接口
 */
@RestController
@RequestMapping
public class CommonController {

    @Resource
    private NovelService novelService;

    /**
     * 搜索
     * @param query 输入值
     * @return 结果列表
     */
    @GetMapping("/search/{query}")
    public Result<List<SearchInfo>> search(@PathVariable String query) {
        // 空白输入
        if (StrUtil.equals(query,"")) return null;

        LambdaQueryWrapper<Novel> wrapper = new LambdaQueryWrapper<>();
        wrapper.select(Novel::getPicture,Novel::getName,Novel::getId,Novel::getAuthor,Novel::getSimple)
                .likeRight(Novel::getName,query)
                .or()
                .likeRight(Novel::getAuthor,query)
                .orderByDesc(Novel::getScore)
                .last("limit 100");
        List<SearchInfo> searchInfos = novelService.list(wrapper).stream()
                .map(SearchInfo::new).toList();
        return Result.ok(searchInfos);
    }

    /**
     * 根据分类查询新书
     * @param type 小说分类
     * @return 小说id+名称
     */
    @GetMapping("/recommend/{type}")
    public Result<List<Recommend>> getRecommend(@PathVariable("type") String type) {
        return novelService.getRecommendByType(type);
    }
}
